package com.aoxin.service.impl.course.systemCourse;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.course.systemCourse.SlfOrderCourseMapper;
import com.aoxin.mapper.course.systemCourse.SlfSyllabusMapper;
import com.aoxin.mapper.opm.SlfClassroomMapper;
import com.aoxin.model.course.SlfOrderCourse;
import com.aoxin.model.course.SlfSyllabus;
import com.aoxin.model.opm.SlfClassroom;
import com.aoxin.service.inter.course.systemCourse.OrderCourseService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.enumerate.OrderCourseStatus;
import com.aoxin.util.enumerate.SyllabusState;

@Service("orderCourseService")
public class OrderCourseServiceImpl implements OrderCourseService {

	@Autowired
	private SlfOrderCourseMapper slfOrderCourseMapper;

	@Autowired
	private SlfSyllabusMapper slfSyllabusMapper;

	@Autowired
	private SlfClassroomMapper slfClassroomMapper;

	@Override
	@Transactional
	public void insertOrderCourse(SlfOrderCourse orderCourse) {
		Assert.notNull(orderCourse, String.format(ErrorUtil.PARAMETER_NULL, "orderCourse"));
		// 上课时间是否冲突
		SlfSyllabus syllabus = this.slfSyllabusMapper.selectByPrimaryKey(orderCourse.getSyllabusId());
		if (SyllabusState.ORDER_ING.getId() != syllabus.getState()) {
			throw new IllegalArgumentException(ErrorUtil.ORDER_COURSE_STATE_ING);
		}
		if (this.slfOrderCourseMapper.checkTime(syllabus.getStartDate(), syllabus.getEndDate(), orderCourse.getChildrenId()) > 0) {
			throw new IllegalArgumentException(ErrorUtil.HAS_ORDERCOURSE_CHECK_TIME);
		}
		// 设置预约状态
		Integer neworderCourseNumber = this.slfOrderCourseMapper.getOrderBySyllabus(orderCourse.getSyllabusId());
		// 查询教室可容纳人数
		SlfClassroom classroom = this.slfClassroomMapper.selectByPrimaryKey(syllabus.getClassroomId());
		Integer acceptanceNum = classroom.getAcceptanceNum();
		// 判断教室容纳人数
		if (neworderCourseNumber >= acceptanceNum) {
			orderCourse.setOrderCourseStatus(OrderCourseStatus.FAIL.getId());
			orderCourse.setRemark(ErrorUtil.ORDER_COURSE_STATUS_FAIL);
		} else if (neworderCourseNumber + 1 == acceptanceNum) {
			orderCourse.setOrderCourseStatus(OrderCourseStatus.SUCCESS.getId());
			this.slfSyllabusMapper.updateState(SyllabusState.FULL.getId(), orderCourse.getSyllabusId());
		} else {
			orderCourse.setOrderCourseStatus(OrderCourseStatus.SUCCESS.getId());
		}
		orderCourse.setOrderDate(new Date());
		this.slfOrderCourseMapper.insertSelective(orderCourse);
	}

	@Override
	@Transactional
	public void deleteOrderCourses(List<Integer> orderCourseIds) {
		Assert.notEmpty(orderCourseIds, String.format(ErrorUtil.PARAMETER_NULL, "orderCourseIds"));
		for (Integer integer : orderCourseIds) {
			SlfOrderCourse orderCourse = this.slfOrderCourseMapper.selectByPrimaryKey(integer);
			if (OrderCourseStatus.CANCEL.getId() != orderCourse.getOrderCourseStatus()) {
				throw new IllegalArgumentException(ErrorUtil.DELETE_ORDERCOURSE);
			}
		}
		this.deleteOrderCourses(orderCourseIds);

	}

	@Override
	@Transactional
	public void updateOrderCourse(SlfOrderCourse orderCourse) {
		Assert.notNull(orderCourse, String.format(ErrorUtil.PARAMETER_NULL, "orderCourse"));
		this.slfOrderCourseMapper.updateByPrimaryKeySelective(orderCourse);
	}

	@Override
	public SlfOrderCourse getOrderCourse(Integer orderCourseId) {
		Assert.notNull(orderCourseId, String.format(ErrorUtil.PARAMETER_NULL, "orderCourseId"));
		return this.slfOrderCourseMapper.selectByPrimaryKey(orderCourseId);
	}

	@Override
	public List<SlfOrderCourse> getOrderCourseList(Map<String, Object> map) {
		return this.slfOrderCourseMapper.getList(map);
	}

	@Override
	@Transactional
	public void cancelOrder(Integer syllabusId, Integer childrenId) {
		Assert.notNull(syllabusId, String.format(ErrorUtil.PARAMETER_NULL, "syllabusId"));
		Assert.notNull(childrenId, String.format(ErrorUtil.PARAMETER_NULL, "childrenId"));

		SlfSyllabus syllabus = this.slfSyllabusMapper.selectByPrimaryKey(syllabusId);
		// 获取课表时间中的年、月、日
		Date syllabusDate = syllabus.getStartDate();
		Date newDate = new Date();
		// 计算距离上课还有多长时间
		long difference = (syllabusDate.getTime() - newDate.getTime()) / 1000 / 60;
		if (difference < 60) {
			throw new IllegalArgumentException(String.format(ErrorUtil.CANCEL_ORDER, difference));
		}
		this.slfOrderCourseMapper.cancelOrder(syllabusId, childrenId);
	}
}
